package com.ruiyi.json.parser;

import java.io.IOException;

import com.ruiyi.baseparser.Group;
import com.ruiyi.baseparser.GroupParser;
import com.ruiyi.baseparser.ParserUtil;
import com.ruiyi.baseparser.RuiyiType;
import com.ruiyi.error.RuiyiCredentialsException;
import com.ruiyi.error.RuiyiException;
import com.ruiyi.error.RuiyiParseException;
import com.ruiyi.json.Json_Product;
import com.ruiyi.json.Json_Result;
import com.ruiyi.util.LogUtil;




public class RuiyiParsers {
	private static final String TAG = "jsonUtil\t";

	public static RuiyiType ParseProduct(String content) {
		Json_Product t;
		try {
			t = (Json_Product) ParserUtil.consume(new ProductParser(), content);

			if (t != null) {
				LogUtil.e(TAG, "parse product success");
				return t;
			}

			Json_Result r = (Json_Result) ParserUtil.consume(
					new ResultParser(), content);
			if (r != null) {
				LogUtil.e(TAG, "parse product failed, parse result success");
				return r;
			}

			LogUtil.e(TAG, "result is null");
			return null;
		} catch (RuiyiCredentialsException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		} catch (RuiyiParseException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		} catch (RuiyiException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		} catch (IOException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public static RuiyiType ParseProductList(String content) {
		Group<Json_Product> t;
		try {
			t = (Group<Json_Product>) ParserUtil.consume(new GroupParser(
					new ProductParser()), content);

			if (t != null) {
				// LogUtil.e(TAG, "parse product list success");
				return t;
			}

			Json_Result r = (Json_Result) ParserUtil.consume(
					new ResultParser(), content);
			if (r != null) {
				// LogUtil.e(TAG,
				// "parse product list failed, parse result success");
				return r;
			}

			LogUtil.e(TAG, "result is null");
			return null;
		} catch (RuiyiCredentialsException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		} catch (RuiyiParseException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		} catch (RuiyiException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		} catch (IOException e) {
			LogUtil.ex(TAG, "", e);
			return null;
		}
	}
}
